package com.ydd.app.manager.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ydd.app.action.CommonAct;
import com.ydd.app.bean.common.NewsDetailBean;
import com.ydd.app.bean.common.NewsTypeBean;
import com.ydd.app.dao.NewsDao;
import com.ydd.app.manager.NewsMng;

@Service
public class NewsMngImpl extends CommonAct implements NewsMng{

	private static final boolean thisStatusIdIsExist (final String newsStatusId){
		 if("0".equals(newsStatusId) || "1".equals(newsStatusId) || "9".equals(newsStatusId)){
			 return true;
		 }
		 return false;
	}

	@Override
	public NewsDetailBean queryNewsDetail(final String newsId) {
		final NewsDetailBean newsDetail = _newsDao.queryNewsDetail(newsId);
		return newsDetail;
	}

	@Override
	public NewsDetailBean updateNews(final String newsId, final String newsDetail,
			final String title, final String newsTypeId,
			final String newsDescribe,final String pictureUrl) {
		final NewsDetailBean newsDetailBean = _newsDao.updateNews(newsId, newsDetail, title, newsTypeId,newsDescribe,pictureUrl);
		return newsDetailBean;
	}

	@Override
	public boolean deleteNews(final String newsId) {
		return _newsDao.deleteNews(newsId);
	}

	@Override
	@Transactional
	public boolean createNews(final String newsDetail, final String title,
			final String newsTypeId ,final String userId,
			final String newsDescribe,final String pictureUrl) {
		return _newsDao.createNews(newsDetail, title, newsTypeId, userId,newsDescribe,pictureUrl);
//		final NewsDetailBean newsDetailBean = _newsDao.createNews(newsDetail, title, newsTypeId, userId,newsDescribe,pictureUrl);
//		return newsDetailBean;
	}

	@Override
	public NewsDetailBean reviewNews(final String newsId,final String statusId,final String reson,final String userId) {
		final NewsDetailBean newsDetail = _newsDao.reviewNews(newsId,statusId,reson,userId);
		return newsDetail;
	}

	
	@Autowired
	private NewsDao _newsDao;

	@Override
	public NewsDetailBean[] queryNewsInBackstage(final int page, final int pageCount,
			final String newsTypeId,final String newsStatusId) {
		String statusId  = null;
		if( thisStatusIdIsExist(newsStatusId) ) {
			statusId = newsStatusId;
		}
		String typeId = null;
		if( _newsDao.querythisnewsTypeIdIsExist(newsTypeId)) {
			typeId = newsTypeId;
		}
		final List<NewsDetailBean> news = _newsDao.queryNewsInBackstage(page, pageCount, typeId,statusId);
		if( null != news ) {
			return news.toArray(new NewsDetailBean[0]);
		}
		else {
			return null;
		}
	}

	@Override
	public int queryNewsCountInBackstage(final String newsTypeId,final String newsStatusId) {
		String statusId  = null;
		if( thisStatusIdIsExist(newsStatusId) ) {
			statusId = newsStatusId;
		}
		String typeId = null;
		if( _newsDao.querythisnewsTypeIdIsExist(newsTypeId)) {
			typeId = newsTypeId;
		}
		return _newsDao.queryNewsCountInBackstage(typeId,statusId);
	}

	@Override
	public NewsTypeBean[] queryNewsTypes() {
		final List<NewsTypeBean> news = _newsDao.queryNewsTypes();
		if( null != news ) {
			return news.toArray(new NewsTypeBean[0]);
		}
		else {
			return null;
		}
	}

	@Override
	public NewsTypeBean[] queryNewestNews(final int pageCount) {
		final List<NewsTypeBean> news = _newsDao.queryNewestNews(pageCount);
		if( null != news ) {
			return news.toArray(new NewsTypeBean[0]);
		}
		else {
			return null;
		}
	}

	@Override
	public NewsDetailBean[] queryNewsInWeb(final int page,final int pageCount,
			final String newsTypeId, final String keyWord) {
		String typeId = null;
		if( _newsDao.querythisnewsTypeIdIsExist(newsTypeId)) {
			typeId = newsTypeId;
		}
		final List<NewsDetailBean> news = _newsDao.queryNewsInWeb(page, pageCount, typeId, keyWord);
		if( null != news ) {
			return news.toArray(new NewsDetailBean[0]);
		}
		else {
			return null;
		}
	}
	
	@Override
	public int queryNewsCountInWeb(final String newsTypeId) {
		String typeId = null;
		if( _newsDao.querythisnewsTypeIdIsExist(newsTypeId)) {
			typeId = newsTypeId;
		}
		return _newsDao.queryNewsCountInWeb(typeId);
	}
}
